<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include('menu.php'); ?>
<script language="javascript">
function CheckReceive(num){
	
	for(i=1;i<=num;i++){
		if(eval("document.formbar.receive"+i+".value")==""){
			alert("กรุณากรอกข้อมูลให้ครบด้วยค่ะ");
			eval("document.formbar.receive"+i+".focus();")
			return false;
		}
	}
	document.formbar.submit();
}

function setFocus(){
	document.formbar.receive1.focus();
	}
</script>

</head>
<body onload="setFocus()">
<?php
include("../connect/connect.php");

if($_GET['action']=='update'){
	$day = date("Y-m-d");
	for($i=0;$i<count($_POST['receive']);$i++){
		$ObjCheck = mysql_query("INSERT INTO stock_bar_cut (stock_bar_id,stock_bar_cut_yesterday,stock_bar_cut_receive,stock_bar_cut_sale,stock_bar_cut_date,stock_bar_cut_status) VALUES ('".$_POST['stock_bar_id'][$i]."','".$_POST['stock_bar_cut_yesterday'][$i]."','".$_POST['receive'][$i]."','0','".$day."','0') ON DUPLICATE KEY UPDATE stock_bar_cut_receive=stock_bar_cut_receive+'".$_POST['receive'][$i]."',stock_bar_cut_status = '0' ");
		//echo $_POST['receive'][$i]."<br>";
	}
	mysql_query("DELETE FROM stock_bar_cut WHERE stock_bar_cut_status = '1'");
}

	$objQuery = mysql_query("SELECT product.prod_name as prod_name,stock_bar.stock_bar_id as stock_bar_id,stock_bar.stock_bar_par as stock_bar_par FROM product,stock_bar WHERE product.prod_id = stock_bar.prod_id ORDER BY product.prod_name");
	$num = mysql_num_rows($objQuery);
?>
<form action="?action=update" name="formbar" method="post" id="formbar">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>จัดการรายการในสต็อกบาร์</h1>
       <div class="buttons"><a onclick="CheckReceive(<?php echo $num;?>)" class="button"><span>บันทึก</span></a><a onclick="location = 'stock_bar.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
            <td class="center">ชื่อสินค้า</td>
            <td width="15%" class="center">Par</td>
            <td width="15%" class="center">ยอดยกมา</td>
            <td width="20%" class="center">รับ</td>
          </tr>
        </thead>
        <tbody>
<?php
	$i=0;
	while($row = mysql_fetch_array($objQuery))
		{
			$i++;
			$Query = mysql_query("SELECT stock_bar_cut_yesterday FROM stock_bar_cut WHERE stock_bar_id = '".$row['stock_bar_id']."' ");
			$RowCut = mysql_fetch_array($Query);
?>  
          <tr>
            <td class="left" style="padding-left:30px"><?php echo $row["prod_name"];?><input type="hidden" name="stock_bar_id[]" value="<?php echo $row['stock_bar_id']; ?>" /></td>
            <td class="right" style="padding-right:50px"><?php echo $row["stock_bar_par"];?></td>
            <td class="right" style="padding-right:50px"><?php if($RowCut['stock_bar_cut_yesterday']==""){ echo "0";} else { echo $RowCut['stock_bar_cut_yesterday']; }?><input type="hidden" name="stock_bar_cut_yesterday[]" value="<?php if($RowCut['stock_bar_cut_yesterday']==""){ echo "0";} else { echo $RowCut['stock_bar_cut_yesterday']; } ?>" /></td>
            <td class="center"><input type="text" name="receive[]" id="receive<?php echo $i; ?>" style="text-align:center;" /></td>
          </tr>
<?php
		}
?>
        </tbody>
      </table>
  </div>
</div>
</form>
</body>
</html>
